package smartclass.dao;

import java.util.ArrayList;

import smartclass.data.ManagerDataBaseHelper;
import smartclass.data.FinalValues;
import smartclass.entity.HomeworkInfo;
import smartclass.present.LogInActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class HomeworkDataDao {

	static HomeworkDataDao homeworkDao;
	ManagerDataBaseHelper db;
	String tableName;

	public HomeworkDataDao() {
		// TODO Auto-generated constructor stub
		db = LogInActivity.db;
		tableName = ManagerDataBaseHelper.HOMEWORK_TABLE;
	}

	public static HomeworkDataDao getInstance() {
		if (homeworkDao == null) {
			homeworkDao = new HomeworkDataDao();
		}
		return homeworkDao;
	}

	// Add Item
	public boolean addHomework(HomeworkInfo hom) {
		SQLiteDatabase data = db.getWritableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");
		if (String.valueOf(hom.getIdTopic()).isEmpty()) {
			return false;
		}
		ContentValues value = new ContentValues();
		value.put(FinalValues.NAME, hom.getName());
		value.put(FinalValues.DATE_CREAT, hom.getDateCreate());
		value.put(FinalValues.ID_TOPIC, hom.getIdTopic());
		value.put(FinalValues.PATH, hom.getPath());
		value.put(FinalValues.HOMEWORK_INFO, hom.getHomeworkInfo());
		data.insert(tableName, null, value);
		data.close();
		return true;
	}

	// Get Item
	public HomeworkInfo getHomework(int id) {
		SQLiteDatabase data = db.getReadableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");
		HomeworkInfo homInfo = new HomeworkInfo();
		Cursor c = null;

		try {
			String[] params = new String[] { String.valueOf(id) };
			String sql = "SELECT * FROM " + tableName + " WHERE "
					+ FinalValues.ID + "=?";
			c = data.rawQuery(sql, params);
			c.moveToFirst();
			homInfo.setId(Integer.parseInt(c.getString(0)));
			homInfo.setName(c.getString(1));
			homInfo.setDateCreate(c.getString(2));
			homInfo.setIdTopic(Integer.parseInt(c.getString(3)));
			homInfo.setPath(c.getString(4));
			homInfo.setHomeworkInfo(c.getString(5));
		} catch (Exception e) {
			// TODO: handle exception
		}
		return homInfo;
	}

	// Get All Item
	public ArrayList<HomeworkInfo> getAllHomework() {
		ArrayList<HomeworkInfo> allHomework = new ArrayList<HomeworkInfo>();
		Cursor c = null;
		SQLiteDatabase data = db.getReadableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");
		String sql = "SELECT * FROM " + tableName;
		c = data.rawQuery(sql, null);
		HomeworkInfo homInfo = null;
		if (c.moveToFirst()) {
			do {
				homInfo = new HomeworkInfo();
				homInfo.setId(Integer.parseInt(c.getString(0)));
				homInfo.setName(c.getString(1));
				homInfo.setDateCreate(c.getString(2));
				homInfo.setIdTopic(Integer.parseInt(c.getString(3)));
				homInfo.setPath(c.getString(4));
				homInfo.setHomeworkInfo(c.getString(5));
			} while (c.moveToNext());
		}
		return allHomework;
	}

	// Edit Item

	public boolean updateHomework(HomeworkInfo hom) {
		SQLiteDatabase data = db.getWritableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");
		ContentValues values = new ContentValues();
		values.put(FinalValues.NAME, hom.getName());
		values.put(FinalValues.DATE_CREAT, hom.getDateCreate());
		values.put(FinalValues.ID_TOPIC, hom.getIdTopic());
		values.put(FinalValues.PATH, hom.getPath());
		values.put(FinalValues.HOMEWORK_INFO, hom.getHomeworkInfo());

		data.update(tableName, values, FinalValues.ID + "=?",
				new String[] { String.valueOf(hom.getId()) });
		data.close();
		return true;
	}

	// Delete Item

	public boolean deleteDirection(int id) {
		String sql = "DELETE FROM " + tableName + " WHERE " + FinalValues.ID
				+ "=" + id;
		db.getWritableDatabase().execSQL(sql);
		return true;
	}

}
